package cn.yitun.chat.yitunservermanagecore.general.manage.common.service.impl;

import cn.yitun.chat.yituncommonmessage.ExceptionEnum;
import cn.yitun.chat.yituncommonmessage.YitunException;
import cn.yitun.chat.yituncommonutil.YitunStringUtil;
import cn.yitun.chat.yitunservercommonlib.constant.Constants;
import cn.yitun.chat.yitunservercommonlib.crud.yitun.CrudServiceImpl;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.common.DictTypeDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.po.common.DictType;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.common.DictTypeVo;
import cn.yitun.chat.yitunserverimbase.dao.common.DictDataDao;
import cn.yitun.chat.yitunserverimbase.dao.common.DictTypeDao;
import cn.yitun.chat.yitunservermanagecore.general.manage.common.service.DictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class DictTypeServiceImpl
		extends CrudServiceImpl<DictTypeDao,DictTypeDto, DictType, DictTypeVo>
		implements DictTypeService {

	@Autowired
	private DictDataDao dictDataDao;

	@Override
	@Transactional
	public int save(DictTypeDto dictType){
		return dao.save(dictType);
	}
	
	@Override
	@Transactional
	public int update(DictTypeDto dictType){
		return dao.update(dictType);
	}


	@Override
	@Transactional
	public int absDel(Long id){
		return dao.absDel(id);
	}


	@Override
	@Transactional
	public int batchAbsDel(Long[] ids){
		return dao.batchAbsDel(ids);
	}

	@Override
	public Boolean checkDictTypeUnique(DictType dict) {
		Long dictId = dict.getId() == null ? -1 : dict.getId();
		DictType dictType = dao.checkDictTypeUnique(dict.getType());
		if (YitunStringUtil.isNotNull(dictType) && dictType.getId() != dictId ){
			return Constants.DICT_TYPE_NOT_UNIQUE;
		}
		return !Constants.DICT_TYPE_NOT_UNIQUE;
	}

	@Override
	public List<DictTypeVo> alllist() {
		return listVO();
	}

	@Transactional
	@Override
	public int absDelDict(Long id) {
		DictType dictType = get(id);
		if(dictType == null )
			throw new YitunException(ExceptionEnum.DELETE_FAILED);
		if (dictDataDao.count(mapModel("type" , dictType.getType()) ) > 0 )
			throw new YitunException(ExceptionEnum.DELETE_FAILED);
		return dao.absDel(id);
	}

}
